package kr.hyunist.calendarproject.DAO;

import java.util.ArrayList;

import kr.hyunist.calendarproject.DTO.MyItem;
import kr.hyunist.calendarproject.MyDBHelper.MyDBHelper;
import android.content.Context;
import android.database.Cursor;
import android.database.SQLException;
import android.database.sqlite.SQLiteDatabase;

public class DAO {
	private MyDBHelper helper;
	private SQLiteDatabase db;
	Cursor cursor;

	private DAO(Context context) {
		helper = new MyDBHelper(context);
		db = helper.getWritableDatabase();
	}

	public static DAO open(Context context) {
		return new DAO(context);
	}

	public void close() {
		helper.close();
	}

/*	// 현재 날짜를 조건으로 검색
	public ArrayList<MyItem> selectTodayScheduleFromDate(String currentDate) {
		ArrayList<MyItem> arList = new ArrayList<>();
		MyItem ListFromDate;

		String sql = "select title, startDate, startTime, alarmInterval, memo from Today where startDate = '" + currentDate + "';";

		cursor = db.rawQuery(sql, null);

		while (cursor.moveToNext()) {
			String title = cursor.getString(0);
			String startDate = cursor.getString(1);
			String startTime = cursor.getString(2);
			int alarmInterval = cursor.getInt(3);
			String memo = cursor.getString(4);
			ListFromDate = new MyItem(title, startDate, startTime, alarmInterval, memo);
			arList.add(ListFromDate);
		}
		return arList;
	}

	//전체 select
	public ArrayList<MyItem> selectTotalSchedule() {
		ArrayList<MyItem> allList = new ArrayList<>();
		MyItem ListAll;

		String sql = "select title, startDate, startTime, alarmInterval, memo from Today order by startDate asc";
		cursor = db.rawQuery(sql, null);

		while (cursor.moveToNext()) {
			String title = cursor.getString(0);
			String startDate = cursor.getString(1);
			String startTime = cursor.getString(2);
			int alarmInterval = cursor.getInt(3);
			String memo = cursor.getString(4);
			ListAll = new MyItem(title, startDate, startTime, alarmInterval, memo);
			allList.add(ListAll);
		}

		return allList;
	}

	//날짜를 select
	public ArrayList<MyItem> selectDateSchedule() {
		ArrayList<MyItem> allDate = new ArrayList<>();
		MyItem ListDate;
		
		String sql = "select title, startDate from Today";
		cursor = db.rawQuery(sql, null);

		while (cursor.moveToNext()) {
			String title = cursor.getString(0);
			String startDate = cursor.getString(1);
			ListDate = new MyItem(title, startDate);
			allDate.add(ListDate);
		}

		return allDate;
	}
	
	//날짜를 select
	public String selectDateSchedule2() {		
		String sql = "select startDate from Today";
		cursor = db.rawQuery(sql, null);
		String startDate;
		
		while (cursor.moveToNext()) {
			startDate = cursor.getString(0);
		}
		return null;
	}
	
	//타이틀을 조건으로 select
	public ArrayList<MyItem> setTextFromTodaySchedule(String todayTitle) {
		ArrayList<MyItem> stList = new ArrayList<>();
		MyItem stListAll;

		String sql = "SELECT title, startDate, startTime, alarmInterval, memo from Today where title='"
				+ todayTitle + "'";
		cursor = db.rawQuery(sql, null);

		if (cursor.moveToNext()) {
			String title = cursor.getString(0);
			String startDate = cursor.getString(1);
			String startTime = cursor.getString(2);
			int alarmInterval = cursor.getInt(3);
			String memo = cursor.getString(4);
			stListAll = new MyItem(title, startDate, startTime, alarmInterval, memo);
			stList.add(stListAll);
		}

		return stList;
	}

	//타이틀과 날짜를 조건으로 select
	public ArrayList<MyItem> setDataFromDateAndTitleSchedule(String todayTitle, String currentDate) {
		ArrayList<MyItem> dtList = new ArrayList<>();
		MyItem stListAll;

		String sql = "SELECT title, startDate, startTime, alarmInterval, memo from Today where title='" + todayTitle + "'and startDate = '" + currentDate + "';";
		cursor = db.rawQuery(sql, null);

		if (cursor.moveToNext()) {
			String title = cursor.getString(0);
			String startDate = cursor.getString(1);
			String startTime = cursor.getString(2);
			int alarmInterval = cursor.getInt(3);
			String memo = cursor.getString(4);
			stListAll = new MyItem(title, startDate, startTime, alarmInterval, memo);
			dtList.add(stListAll);
		}

		return dtList;
	}	
	
	
	
	//업데이트
	public boolean updateSchedule(String title, String startDate, String startTime, int alarmInterval, String memo, String todayTitle, String todayStartDate) {

		String sql = "update today set title = '" + title + "' , startDate = '" + startDate + "', startTime = '" + startTime
				+ "', alarmInterval = '" + alarmInterval + "', memo = '"
				+ memo + "' where title = '" + todayTitle + "' and startDate = '" + todayStartDate+ "';";

		try {
			db.execSQL(sql);
			
		} catch (SQLException e) {

			return false;
		}

		return true;
	}

	//insert
	public boolean insertSchedule(String title, String startDate, String startTime, int alarmInterval, String memo) {
		
		String sql = "insert into today values(" + "null, " + "'" + title
				+ "', " + "'" + startDate + "', " + "'" + startTime + "', "
				+ "'" + alarmInterval + "', " + "'" + memo + "'" + ");";

		try {
			db.execSQL(sql);
		} catch (SQLException e) {
			return false;
		}
		return true;
	}

	//delete
	public boolean deleteSchedule(String todayTitle, String todayStartDate) {
		String sql = "delete from today where title='" + todayTitle + "' and startDate='" + todayStartDate + "';";

		try {
			db.execSQL(sql);
		} catch (SQLException e) {
			return false;
		}
		return true;
	}*/

}
